/*
 * @Author: szx
 * @Date: 2022-03-08 14:37:13
 * @LastEditTime: 2022-03-08 15:10:26
 * @Description:
 * @FilePath: \leetcode\1700-1799\1754\1754.js
 */
/**
 * @param {string} word1
 * @param {string} word2
 * @return {string}
 */
var largestMerge = function (word1, word2) {
    let i = 0;
    let j = 0;
    const merge = [];
    while (i < word1.length && j < word2.length) {
        if (word1[i] > word2[j]) merge.push(word1[i++]);
        else if (word1[i] < word2[j]) merge.push(word2[j++]);
        else {
            merge.push(word1[i]);
            let m = i + 1;
            let n = j + 1;
            let flag = false;
            while (m < word1.length && n < word2.length) {
                if (word1[m] < word2[n]) {
                    j++;
                    flag = true;
                    break;
                } else if (word1[m] > word2[n]) {
                    i++;
                    flag = true;
                    break;
                }
                m++;
                n++;
            }
            if (flag == false) {
                if (m < word1.length) i++;
                else if (n < word2.length) j++;
                else i++;
            }
        }
    }
    if (i < word1.length) merge.push(word1.substring(i));
    if (j < word2.length) merge.push(word2.substring(j));
    return merge.join('');
};
var largestMerge = function (word1, word2) {
    let i = 0,
        j = 0;
    let res = '';
    while (i < word1.length && j < word2.length) {
        if (word1.substr(i) < word2.substr(j)) {
            res += word2[j];
            j += 1;
        } else {
            res += word1[i];
            i += 1;
        }
    }
    res += word1.substr(i) + word2.substr(j);
    return res;
};
console.log(largestMerge('cabaa', 'bcaaa'));
console.log(largestMerge('abcabc', 'abdcaba'));
console.log(largestMerge('abc', 'abd'));
// console.log(largestMerge('abc', 'abcd'));

// console.log(largestMerge('ddwdddddddddddddwwddddddwddw', 'wwwwwwwwwddwwdwwwwwwwwwwwwwww')==);
// console.log('wwwwwwwwwddwwdwwwwwwwwwwwwwwwddwdddddddddddddwwddddddwddw')
// console.log(largestMerge('ddwdddddddddddddwwddddddwddw', 'ddwwdwwwwwwwwwwwwwww'));
